package com.sun.androidexerciseproject.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.ViewGroup;

import com.sun.androidexerciseproject.widget.R;

/**
 * Created by Administrator on 2018/3/27 0027.
 */

public class CustomView extends ViewGroup {
    private Bitmap mImageSrc;
    private float mImageAspectRadio;
    private float mImageAlpha;
    private int mImagePaddingLeft;
    private int mImagePaddingRight;
    private int mImagePaddingTop;
    private int mImagePaddingBottom;
    private int mImageScaleType;
    private String mTitleText;
    private int mTitleTextSize;
    private int mTitleTextColor;
    private int mTitlePaddingLeft;
    private int mTitlePaddingRight;
    private int mTitlePaddingTop;
    private int mTitlePaddingBottom;

    private static final int SCALE_TYPE_FILLXY = 0;
    private static final int SCALE_TYPE_CENTER = 1;

    private Paint mPaint;
    private Paint mTitlePaint;

    private int mViewWidth;
    private int mViewHeight;


    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttar(context, attrs, defStyleAttr);
    }


    private void initAttar(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, defStyleAttr, 0);
        int size = a.getIndexCount();
        for (int i = 0; i < size; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.CustomView_imageSrc:
                    mImageSrc = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0));
                    break;
                case R.styleable.CustomView_imageAlpha:
                    mImageAlpha = a.getFloat(attr, 1.0f);
                    break;
                case R.styleable.CustomView_imageAspectRatio:
                    mImageAspectRadio = a.getFloat(attr, 1.0f);
                    break;
                case R.styleable.CustomView_imagePaddingLeft:
                    mImagePaddingLeft = a.getInt(attr, 0);
                    break;
                case R.styleable.CustomView_imagePaddingRight:
                    mImagePaddingRight = a.getInt(attr, 0);
                    break;
                case R.styleable.CustomView_imagePaddingTop:
                    mImagePaddingTop = a.getInt(attr, 0);
                    break;
                case R.styleable.CustomView_imagePaddingBottom:
                    mImagePaddingBottom = a.getInt(attr, 0);
                    break;
                case R.styleable.CustomView_imageScaleType:
                    mImageScaleType = a.getInt(attr, SCALE_TYPE_CENTER);
                    break;
                case R.styleable.CustomView_titleText:
                    mTitleText = a.getString(attr);
                    break;
                case R.styleable.CustomView_titleTextColor:
                    mTitleTextColor = a.getColor(attr, 0x00000000);
                    break;
                case R.styleable.CustomView_titleTextSize:
                    mTitleTextSize = a.getInt(attr, 0);
                    break;
                case R.styleable.CustomView_titlePaddingLeft:
                    mTitlePaddingLeft = a.getInt(attr, 0);
                    break;
                case R.styleable.CustomView_titlePaddingRight:
                    mTitlePaddingRight = a.getInt(attr, 0);
                    break;
                case R.styleable.CustomView_titlePaddingTop:
                    mTitlePaddingTop = a.getInt(attr, 0);
                    break;
                case R.styleable.CustomView_titlePaddingBottom:
                    mTitlePaddingBottom = a.getInt(attr, 0);
                    break;
            }
        }
        a.recycle();
    }

    private void init(Context context) {
        mPaint = new Paint();
        mTitlePaint = new Paint();

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }
}
